package com.txzt.gaea.upgrade.result.controller;

import com.txzt.base.core.RtnInfo;
import com.txzt.gaea.aspect.annotation.SysDataPermission;
import com.txzt.gaea.business.util.TranslateUtil;
import com.txzt.gaea.common.annotation.EntityCommonValue;
import com.txzt.gaea.common.base.controller.BaseController;
import com.txzt.gaea.common.enums.IsDeleteEnum;
import com.txzt.gaea.upgrade.result.domain.model.ResultInfo;
import com.txzt.gaea.upgrade.result.service.IResultInfoService;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/result/info")
public class ResultInfoController extends BaseController {

    @Autowired
    private IResultInfoService resultInfoService;


    /**
     * 分页查询
     *
     * @param resultInfo
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/list")
    public RtnInfo<?> pageList(@RequestParam(defaultValue = "1") Integer pageNumber,
                                      @RequestParam(defaultValue = "10") Integer pageSize, ResultInfo resultInfo) {
        PageQuery<ResultInfo> pageList = resultInfoService.pageQuery(new PageQuery<>(pageNumber, pageSize, resultInfo));
        pageList.getList().forEach(item ->{
            //翻译提报人
            item.setCreateByName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译负责人
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译提报部门
            item.setDeptName(TranslateUtil.getDepartmentName(item.getDeptId()));
            //翻译承担部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 查询待审核数量(成果申请)
     * @param resultInfo
     * @return
     */
    @SysDataPermission
    @GetMapping(value = "/listCheckCount")
    public RtnInfo<?> listCheckCount(ResultInfo resultInfo) {
        Long count = resultInfoService.listCheckCount(resultInfo);
        return RtnInfo.success(count);
    }


    /**
     * 分页查询
     *
     * @param resultInfo
     * @return
     */
    @GetMapping(value = "/pageInfo")
    public RtnInfo<?> info(@RequestParam(defaultValue = "1") Integer pageNumber,
                           @RequestParam(defaultValue = "10") Integer pageSize, ResultInfo resultInfo) {
        PageQuery<ResultInfo> pageList = resultInfoService.queryInfo(new PageQuery<>(pageNumber, pageSize, resultInfo));
        pageList.getList().forEach(item ->{
            //翻译借阅人
            item.setCreateByName(TranslateUtil.getUserName(item.getCreateBy()));
            //翻译负责人
            item.setProjectLeaderName(TranslateUtil.getUserName(item.getProjectLeader()));
            //翻译承担部门
            item.setDepartName(TranslateUtil.getDepartmentName(item.getDepartId()));
        });
        return RtnInfo.success(pageList);
    }

    /**
     * 查询全信息
     * @return
     */
    @GetMapping(value = "/info")
    public RtnInfo<?> info() {
        List<ResultInfo> list = resultInfoService.query();
        return RtnInfo.success(list);
    }

    /**
     * 新增
     * @param resultInfo
     * @return
     */
    @PostMapping(value = "/add")
    @EntityCommonValue(operation = "CREATE")
    public RtnInfo<?> add(@RequestBody ResultInfo resultInfo) {
        int count = resultInfoService.addResultInfo(resultInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 更新
     * @param resultInfo
     * @return
     */
    @PutMapping(value = "/edit")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> edit(@RequestBody ResultInfo resultInfo) {
        int count = resultInfoService.editResultInfo(resultInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }

    /**
     * 撤回
     * @param resultInfo
     * @return
     */
    @PutMapping(value = "/revocation")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> revocation(@RequestBody ResultInfo resultInfo) {
        int count = resultInfoService.revocationResultInfo(resultInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @EntityCommonValue(operation = "UPDATE")
    public RtnInfo<?> delete(@RequestParam Long id) {
        ResultInfo resultInfo = new ResultInfo();
        resultInfo.setId(id);
        resultInfo.setIsDelete(IsDeleteEnum.DELETE.getCode());
        int count = resultInfoService.updateById(resultInfo);
        if (count > 0) {
            return RtnInfo.SUCCESS;
        } else {
            return RtnInfo.SERVER_ERROR;
        }
    }


}
